/*
 * Yabumi, a 2d scrolling shoot-em-up game.
 * Copyright (C) 2003 Gemma Teruaki <terugen@yabumi.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 */

#include <math.h>
#include <stdbool.h>
#include "../taskmanager.h"
#include "../sprite.h"
#include "../video.h"
#include "../util.h"
#include "missile.h"
#include "flame.h"
#include "homing.h"
#include "explosion.h"
#include "smoke.h"
#include "item.h"
#include "radar.h"
#include "hitcircle.h"
#include "player.h"

extern _list_header *active_task_list_header, *expired_task_list_header;
extern _list_header hitcircle_list_header;
_list_header player_list_header;

void Player_Init()
{
  Create_list(&player_list_header);
}

void Player_Free()
{
  Free_list(&player_list_header);
}

_Player *Player_Create(int x, int y, int dx, int dy, int angle)
{
  _list_item *list_item;
  list_item = (_list_item *)pmalloc(sizeof(_list_item));
  Add_item_last(&player_list_header, list_item);
  _Player *player = &list_item->data.Player;
  player->root = list_item;

  player->x = x;
  player->y = y;
  player->dx = dx;
  player->dy = dy;
  player->angle = angle;
  int i;
  for (i = 20; i < PLAYER_THRUST_TIME; i++) player->accel[i] = 100;
  for (i = 10; i < 20; i++) player->accel[i] = 80;
  for (i = 3; i < 10; i++) player->accel[i] = 60;
  for (i = 0; i < 3; i++) player->accel[i] = 80;
  player->shield = PLAYER_SHIELD;
  player->weapon = __weapon_missile;
  player->waits.fire = false;
  player->waits.thrust = false;
  player->waits.fox1 = false;
  player->waits.lockon = false;
  player->tasks.fire = false;
  player->tasks.thrust = false;
  player->tasks.rotate_left = false;
  player->tasks.rotate_right = false;
  player->tasks.was_hit = __hit_no;
  return player;
}

task Player_Add(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];
  Task_Add(Player_Draw, player, NULL, INFINITY_COUNT);
  Task_Add(Radar_Draw, player, NULL, INFINITY_COUNT);
  Task_Add(Hitcircle_Add,
	   Hitcircle_Create(player, __hitcircle_player, 8, __visible, 0, 0),
	   player, ONETIME_COUNT);
  Task_Add(Player_Move, player, NULL, INFINITY_COUNT);
}

task Player_Move(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];

  if (player->shield <= 0) {
    optv->count = KILL_COUNT;
    Task_Add(Player_Kill, player, NULL, PLAYER_KILL_TIME);
    return;
  };
  /* Move Player */
  /* accurate but too slow
   *double norm;
   *double k;
   *norm = distance(0,0,player->dx,player->dy);
   *k = PLAYER_MAX_SPEED / norm;
   *if ( k < 1.0) {
   * player->dx *= k;
   * player->dy *= k;
   *};
   */
  if (player->dx > PLAYER_MAX_SPEED) player->dx = PLAYER_MAX_SPEED;
  if (player->dx < -1 * PLAYER_MAX_SPEED) player->dx = -1 * PLAYER_MAX_SPEED;
  if (player->dy > PLAYER_MAX_SPEED) player->dy = PLAYER_MAX_SPEED;
  if (player->dy < -1 * PLAYER_MAX_SPEED) player->dy = -1 * PLAYER_MAX_SPEED;
  cap_angle(&player->angle);
  player->x += player->dx >> 8;
  player->y += player->dy >> 8;

  /* check lockon */
  player->tasks.fox1 = false;
  int sight_x, sight_y;
  _list_item *list_item = hitcircle_list_header.next->next;
  _Hitcircle *hitcircle;
  if (player->waits.lockon != true) {
    sight_x = player->x + LOCKON_SIGHT_DISTANSE * cos(player->angle * RM_PI);
    sight_y = player->y + LOCKON_SIGHT_DISTANSE * sin(player->angle * RM_PI);
    while (list_item != hitcircle_list_header.prev) {
      hitcircle = &list_item->data.Hitcircle;
      if (distance(sight_x, sight_y, hitcircle->x, hitcircle->y) <= hitcircle->radius + PLAYER_LOCKON_DISTANCE) {
	/*Lockon!*/
	player->waits.lockon = true;
	Task_Add(Player_Lockon_Hitcircle, player, hitcircle, PLAYER_LOCKON_LIFETIME);
	Task_Add(Player_Lockon_Hitcircle_Wait, player, NULL, ONETIME_COUNT + 5);
	break;
      };
      list_item = list_item->next;
    };
  };
  if (player->tasks.fire == true) {
    player->tasks.fire = false;
    if (player->waits.fire != true) {
      player->waits.fire = true;
      Task_Add(Player_Fire, player, NULL, ONETIME_COUNT);
    };
  };
  if (player->tasks.thrust == true) {
    player->tasks.thrust = false;
    if (player->waits.thrust != true) {
      player->waits.thrust = true;
      Task_Add(Player_Thrust, player, NULL, PLAYER_THRUST_TIME);
    };
  };
  if (player->tasks.rotate_left == true) {
    player->tasks.rotate_left = false;
    Task_Add(Player_Rotate_Left, player, NULL, PLAYER_ROTATE_TIME);
  };
  if (player->tasks.rotate_right == true) {
    player->tasks.rotate_right = false;
    Task_Add(Player_Rotate_Right, player, NULL, PLAYER_ROTATE_TIME);
  };
  if (player->tasks.was_hit != __hit_no) {
    Task_Add(Player_was_Hit, player, NULL, ONETIME_COUNT);
  };
}

task Player_Draw(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];
  /* Draw Player */
  int i;
  SDL_Surface *image;
  i = (optv->count / -10) % 3;
  cap_angle(&player->angle);
  image = Sprite_Player[i][player->angle];
  Sprite_Add(player->x, player->y, Z_ORDER_D2, image);
  
  if (player->shield <= PLAYER_SHIELD * 0.5) {
    Task_Add(Smoke_Draw,
	     Smoke_Create(player->x + 10, player->y),
	     NULL, ONETIME_COUNT + 3);
  };
  if (player->shield <= PLAYER_SHIELD * 0.3) {
    Task_Add(Smoke_Draw,
	     Smoke_Create(player->x, player->y + 10),
	     NULL, ONETIME_COUNT + 7);
  };
}

task Player_Fire(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];

  switch (player->weapon) {
  case __weapon_missile:
    Task_Add(Player_Fire_Missile, player, NULL, PLAYER_FIRE_MISSILE_WAIT);
    break;
  case __weapon_flame:
    Task_Add(Player_Fire_Flame, player, NULL, PLAYER_FIRE_FLAME_WAIT);
    break;
  };
}

task Player_Fire_Missile(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];

  if (optv->count == PLAYER_FIRE_MISSILE_WAIT) {
    Task_Add(Missile_Add, 
	     Missile_Create(player->x, player->y, player->dx, player->dy, player->angle), 
	     NULL, ONETIME_COUNT);
  };
  if (optv->count == ONETIME_COUNT) player->waits.fire = false;
}

task Player_Fire_Flame(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];

  if (optv->count == PLAYER_FIRE_FLAME_WAIT) {
    Task_Add(Flame_Add, 
	     Flame_Create(player->x, player->y, player->dx, player->dy, player->angle), 
	     NULL, ONETIME_COUNT);
  };
  if (optv->count == ONETIME_COUNT) player->waits.fire = false;
}

task Player_Rotate_Left(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];

  player->angle += PLAYER_ROTATE_SPEED;
}

task Player_Rotate_Right(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];

  player->angle -= PLAYER_ROTATE_SPEED;
}

task Player_Thrust(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];

  int i;
  SDL_Surface *image;
  i = ((optv->count * 5) / PLAYER_THRUST_TIME) % 5;
  cap_angle(&player->angle);
  image = Sprite_Player_Thrust[i][player->angle];
  Sprite_Add(player->x, player->y, Z_ORDER_D2, image);

  player->dx += player->accel[optv->count] * cos(player->angle * RM_PI);
  player->dy += player->accel[optv->count] * sin(player->angle * RM_PI);

  if (optv->count == PLAYER_THRUST_WAIT) player->waits.thrust = false;
}

task Player_was_Hit(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];
  
  switch (player->tasks.was_hit) {
  case __hit_missile:
    Task_Add(Player_was_Hit_by_Missile, player, NULL, ONETIME_COUNT + 10);
    break;
  case __hit_flame:
    Task_Add(Player_was_Hit_by_Flame, player, NULL, ONETIME_COUNT + 10);
    break;
  case __hit_item_missile:
    Task_Add(Player_was_Hit_by_Item_Missile, player, NULL, ONETIME_COUNT);
    break;
  case __hit_item_flame:
    Task_Add(Player_was_Hit_by_Item_Flame, player, NULL, ONETIME_COUNT + 400);
    break;
  default:
    break;
  };
  player->tasks.was_hit = 0;
}

task Player_was_Hit_by_Missile(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];
  
  if (optv->count % 2 == 0) {
    player->x += 3.0 * cos((player->angle + 90) * RM_PI);
    player->y += 3.0 * sin((player->angle + 90) * RM_PI);
  };
  if (optv->count % 2 == 1) {
    player->x += 3.0 * cos((player->angle - 90) * RM_PI);
    player->y += 3.0 * sin((player->angle - 90) * RM_PI);
  };

  if (optv->count == ONETIME_COUNT) {
    player->shield -= MISSILE_POWER;
  };
}

task Player_was_Hit_by_Flame(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];
  
  if (optv->count % 2 == 0) {
    player->x += 3.0 * cos((player->angle + 90) * RM_PI);
    player->y += 3.0 * sin((player->angle + 90) * RM_PI);
  };
  if (optv->count % 2 == 1) {
    player->x += 3.0 * cos((player->angle - 90) * RM_PI);
    player->y += 3.0 * sin((player->angle - 90) * RM_PI);
  };

  if (optv->count == ONETIME_COUNT) {
    player->shield -= FLAME_POWER;
  };
}

task Player_was_Hit_by_Item_Missile(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];
  
  if (optv->count == ONETIME_COUNT) {
    player->weapon = __weapon_missile;
  };
}

task Player_was_Hit_by_Item_Flame(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];
  
  if (optv->count > ONETIME_COUNT) {
    player->weapon = __weapon_flame;
  };
  if (optv->count == ONETIME_COUNT) {
    player->weapon = __weapon_missile;
  };
}

task Player_Lockon_Hitcircle(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];
  _Hitcircle *hitcircle;
  hitcircle = (_Hitcircle *)optv->socket[1];

  SDL_Surface *image;
  int i;
  i = (optv->count / 5) % 3;
  image = Sprite_Radar_Sight[i];
  Sprite_Add(hitcircle->x, hitcircle->y, Z_ORDER_D1, image);

  if (player->tasks.fox1 == true) {
    player->tasks.fox1 = false;
    if (player->waits.fox1 != true) {
      player->waits.fox1 = true;
      /* Fire! Homing */
      Task_Add(Homing_Add, 
	       Homing_Create(player->x, player->y, player->dx, player->dy, player->angle + 180),
	       hitcircle, ONETIME_COUNT);
      Task_Add(Player_Fire_Homing_Wait, player, NULL, ONETIME_COUNT + 5);
      optv->count = KILL_COUNT;
    };
  };
};

task Player_Fire_Homing_Wait(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];
  if (optv->count == ONETIME_COUNT) player->waits.fox1 = false;
}

task Player_Lockon_Hitcircle_Wait(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];
  if (optv->count == ONETIME_COUNT) player->waits.lockon = false;
}

task Player_Kill(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];

  /* Move Player */
  player->x += player->dx >> 8;
  player->y += player->dy >> 8;
  player->dx *= 0.9;
  player->dy *= 0.9;
  cap_angle(&player->angle);

  if (optv->count == ONETIME_COUNT + 1) {
    Task_Add(Explosion_Draw, 
	     Explosion_Create(player->x, player->y), 
	     NULL, ONETIME_COUNT + 7);
  };

  int i;
  _list_item *list_item;
  _task_item *task_item;

  if (optv->count == ONETIME_COUNT) {
    list_item = active_task_list_header->next->next;
    while (list_item != active_task_list_header->prev) {
      task_item = &list_item->data.task_item;
      for (i = 0; i < MAX_SOCKET; i++) {
	if (task_item->option.socket[i] == player) {
	  if (task_item->fp == Hitcircle_Move) {
	    task_item->option.count = ONETIME_COUNT;
	  } else {
	    task_item->option.count = KILL_COUNT;
	  };
	};
      };
      list_item = list_item->next;
    };
    list_item = expired_task_list_header->next->next;
    while (list_item != expired_task_list_header->prev) {
      task_item = &list_item->data.task_item;
      for (i = 0; i < MAX_SOCKET; i++) {
	if (task_item->option.socket[i] == player) {
	  if (task_item->fp == Hitcircle_Move) {
	    task_item->option.count = ONETIME_COUNT;
	  } else {
	    task_item->option.count = KILL_COUNT;
	  };
	};
      };
      list_item = list_item->next;
    };
/*     Kill_item(&player_list_header, (_list_item *)player->root); */
/*     pfree(player->root); */
  };
}
